package com.scut.miaosha.controller;

import com.scut.miaosha.Result.Result;
import com.scut.miaosha.domain.MiaoshaUser;
import com.scut.miaosha.redis.GoodsKey;
import com.scut.miaosha.redis.RedisService;
import com.scut.miaosha.service.GoodsService;
import com.scut.miaosha.service.MiaoshaUserService;
import com.scut.miaosha.vo.GoodsDetailVo;
import com.scut.miaosha.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    MiaoshaUserService miaoshaUserService;

    @Autowired
    RedisService redisService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;

    @Autowired
    ApplicationContext applicationContext;

// QPS : 1289

    @RequestMapping(value = "/to_list" , produces = "text/html")
    @ResponseBody
    public String toGoodsList(HttpServletRequest request,HttpServletResponse response,Model model, MiaoshaUser user){
        //查询商品列表

        model.addAttribute("user",user);
        String html = redisService.get(GoodsKey.getGoodsList,"",String.class);
        if(html != null)
        {
            return html;
        }
        List<GoodsVo> goodsVoList = goodsService.ListGoodsVo();
        model.addAttribute("goodsList",goodsVoList);
        //去缓存

        //手动渲染
        SpringWebContext context = new SpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(), applicationContext);
        html = thymeleafViewResolver.getTemplateEngine().process("goods_list",context);
        if(html != null)
        {
            redisService.set(GoodsKey.getGoodsList,"",html);
        }
        return html;
    }

    @RequestMapping(value = "/to_detail2/{goodsId}" , produces = "text/html")
    @ResponseBody
    public String detail2(HttpServletRequest request,HttpServletResponse response,Model model, MiaoshaUser user, @PathVariable("goodsId")long goodsId){
        model.addAttribute("user",user);
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
        model.addAttribute("goods",goods);

        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int miaoshaStatus = 0;
        int remainSeconds = 0;

        if(now < startAt){  //秒杀还没开始
            miaoshaStatus = 0;
            remainSeconds = (int)((startAt - now)/1000);
        }else if(now > endAt){ //秒杀已经结束
            miaoshaStatus = 2;
            remainSeconds = -1;
        }else{ //秒杀进行中
            miaoshaStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("miaoshaStatus",miaoshaStatus);
        model.addAttribute("remainSeconds",remainSeconds);


        String html = redisService.get(GoodsKey.getGoodsDetail,""+goodsId,String.class);
        if(html != null)
        {
            return html;
        }

        //手动渲染
        SpringWebContext context = new SpringWebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap(), applicationContext);
        html = thymeleafViewResolver.getTemplateEngine().process("goods_detail",context);
        if(html != null)
        {
            redisService.set(GoodsKey.getGoodsDetail,""+goodsId,html);
        }
        return html;
    }

    @RequestMapping(value = "/detail/{goodsId}")
    @ResponseBody
    public Result<GoodsDetailVo> detail(HttpServletRequest request, HttpServletResponse response, Model model, MiaoshaUser user, @PathVariable("goodsId")long goodsId){
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);

        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();

        int miaoshaStatus = 0;
        int remainSeconds = 0;

        if(now < startAt){  //秒杀还没开始
            miaoshaStatus = 0;
            remainSeconds = (int)((startAt - now)/1000);
        }else if(now > endAt){ //秒杀已经结束
            miaoshaStatus = 2;
            remainSeconds = -1;
        }else{ //秒杀进行中
            miaoshaStatus = 1;
            remainSeconds = 0;
        }
        GoodsDetailVo vo = new GoodsDetailVo();
        vo.setGoods(goods);
        vo.setUser(user);
        vo.setMiaoshaStatus(miaoshaStatus);
        vo.setRemainSeconds(remainSeconds);
        return Result.success(vo);
    }
}
